package main

//#link: https://leetcode-cn.com/problems/liang-ge-lian-biao-de-di-yi-ge-gong-gong-jie-dian-lcof/
import (
	"fmt"
	. "whoops/leetcode/common"
)

func main() {
	headA := ArrayToList([]int{4, 1, 8, 4, 5})
	headB := ArrayToList([]int{5, 0, 1, 8, 4, 5})
	fmt.Println(getIntersectionNode(headA, headB))
}

func getIntersectionNode(headA, headB *ListNode) *ListNode {
	if headA == nil || headB == nil {
		return nil
	}
	pa, pb := headA, headB
	for pa != pb {
		if pa == nil {
			pa = headB
		} else {
			pa = pa.Next
		}
		if pb == nil {
			pb = headA
		} else {
			pb = pb.Next
		}
	}
	return pa
}
